package com.tzu.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * UI工具
 */
public class UITools {

    public static String lookfeel_nimbus = javax.swing.plaf.nimbus.NimbusLookAndFeel.class.getName();
    public static String lookfeel_windows = com.sun.java.swing.plaf.windows.WindowsLookAndFeel.class.getName();
    public static String lookfeel_crossPlatform = UIManager.getCrossPlatformLookAndFeelClassName();
    public static String lookfeel_system = UIManager.getSystemLookAndFeelClassName();

    private static void test() throws Exception {
        UIManager.setLookAndFeel(lookfeel_nimbus);
    }

    /**
     * 修改字体大小
     */
    public static Font makeFont(Font ofont, int style, int size) {
        String name = ofont.getFontName();
        return new Font(name, style, size);
    }

    /**
     * 显示错误信息
     */
    public static void showDialogError(Component component, String title, String message) {
        JOptionPane.showMessageDialog(component, message, title, JOptionPane.ERROR_MESSAGE);
    }

    /**
     * 显示警告信息
     */
    public static void showDialogWaring(Component component, String title, String message) {
        JOptionPane.showMessageDialog(component, message, title, JOptionPane.WARNING_MESSAGE);
    }

    /**
     * 显示信息
     */
    public static void showDialogInfo(Component component, String title, String message) {
        JOptionPane.showMessageDialog(component, message, title, JOptionPane.INFORMATION_MESSAGE);
    }

    /**
     * 显示问题消息
     */
    public static void showDialogQuest(Component component, String title, String message) {
        JOptionPane.showMessageDialog(component, message, title, JOptionPane.QUESTION_MESSAGE);
    }

    /**
     * 复制到剪切板
     */
    public static void copyToClipboard(String info) {
        // 得到剪切板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 封装文本内容
        Transferable trans = new StringSelection(info);
        // 把文本内容设置到系统剪贴板
        clipboard.setContents(trans, null);
    }

    /**
     * 打开路径
     */
    public static void openDirPath(String dirPath) {
        try {
            if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().open(new File(dirPath));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 拖拽处理
     */
    public static File[] executeDropFiles(Transferable transferable) throws IOException, UnsupportedFlavorException {
        List<File> filelist = new ArrayList<>();

        DataFlavor[] dataFlavors = transferable.getTransferDataFlavors();
        for (DataFlavor dataFlavor : dataFlavors) {
            Object obj = transferable.getTransferData(dataFlavor);
            if (obj instanceof List) {
                List<Object> list = (List<Object>) obj;
                for (Object file : list) {
                    if (file instanceof File) {
                        filelist.add((File) file);
                    }
                }
            } else {
                if (obj instanceof File) {
                    filelist.add((File) obj);
                }
            }
        }

        Object o = transferable.getTransferData(DataFlavor.javaFileListFlavor);

        String filepath = o.toString();
        if (filepath.startsWith("[")) {
            filepath = filepath.substring(1);
        }
        if (filepath.endsWith("]")) {
            filepath = filepath.substring(0, filepath.length() - 1);
        }
        // 转为数组
        File[] objlist = filelist.toArray(new File[filelist.size()]);
        return objlist;
    }

}
